#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<vector>

using namespace std;

struct TreeNode {
     int val;
     TreeNode *left;
     TreeNode *right;
     TreeNode() : val(0), left(nullptr), right(nullptr) {}
     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution {
public:
    int pseudoPalindromicPaths(TreeNode* root) {
        vector<int> cnt(10);
        return dfs(root, cnt);
    }

    int dfs(TreeNode* root, vector<int>& cnt)
    {
        if (!root)   return 0;
        cnt[root->val]++;

        int res = 0;
        if (!root->left && !root->right)
        {
            if (judge(cnt))
                res = 1;
        }
        else
            res = dfs(root->left, cnt) + dfs(root->right, cnt);
        cnt[root->val]--;
        return res;
    }

    bool judge(vector<int>& cnt)
    {
        int c = 0;
        for (auto it : cnt)
            if (it % 2)
                c++;
        return c <= 1;
    }
};